package com.teddy.model.abstracts;

import java.io.File;
import java.io.IOException;
import java.util.Observable;

import com.hp.hpl.jena.rdf.model.Model;
import com.teddy.model.Deserializer;
import com.teddy.model.skos.DCModel;
import com.teddy.util.Constants;
import com.teddy.util.FileManager;

public class SemanticObject extends Observable{
	private String name;
	private String URI;
	private Model model;
	private String selectedLanguage;
	private String format;
	private DCModel dcModel;
	public boolean isThesaurus;
	public String modelUri;
	
	public SemanticObject(String name, String URI){
		this.name = name;
		this.URI = URI;
	}
	
	public SemanticObject(String name, String URI, boolean isThesaurus){
		this.name = name;
		this.URI = URI;
		this.isThesaurus = isThesaurus;
	}
	
	public SemanticObject(String URI){
		this.URI = URI;
		try {
			loadModel(true);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setURI(String URI) {
		this.URI = URI;
	}
	public String getURI() {
		return URI;
	}

	public void setModel(Model model) {
		this.model = model;
	}

	public Model getModel() {
		return model; 
	}
	
	private Model loadModel(String path){
		Model model = Deserializer.deserializeModel(path);
		return model;
	}
	
	public void loadModel(boolean decideModelType) throws IOException{
		model = Deserializer.deserializeModel(URI);
		if (decideModelType)
			decideModelType();
		loadDCMetadata();
	}
	
	private void decideModelType(){
		if (URI.endsWith(Constants.THESAUR_EXT))
			isThesaurus = true;
	}
	
	public void loadDCMetadata() throws IOException{
		String dcPath;
		if (isThesaurus)
			dcPath = URI.replaceFirst(Constants.THESAUR_EXT, Constants.DC_MD_EXT);
		else
			dcPath = URI.replaceFirst(Constants.XML_EXT, Constants.DC_MD_EXT);
		File dcFile = new File (dcPath);
		
		if (dcFile.exists()){
			dcModel = new DCModel(loadModel(dcPath),dcPath);
		}
		
		if (dcModel == null){
			if (isThesaurus){
				String dcContent = null;
				try {
					dcContent = FileManager.convertStreamToString(this.getClass().getResourceAsStream("../../../../" + Constants.DC_TEMPLATE));
				} catch (Exception e) {
					e.printStackTrace();
				}
				name = (new File(URI)).getName().replace(Constants.THESAUR_EXT, "") ;
				dcContent = dcContent.replace(Constants.REQUIRED_DC_TITLE, name);
				FileManager.writeFile(dcPath, dcContent);
				
				dcModel = new DCModel(loadModel(dcPath),dcPath);
			}
			else{
				if ((new File(URI)).getName().endsWith(Constants.THESAUR_EXT))
					name = (new File(URI)).getName().replace(Constants.THESAUR_EXT, "") ;
				else
					name = (new File(URI)).getName().replace(Constants.XML_EXT, "") ;
			}
		}else{
			setName(dcModel.getTitle());
			setSelectedLanguage(dcModel.getSelectedLanguage());
		}
	}
	
	
	public void delete(){
		this.URI = null;
		this.setChanged();
		notifyObservers();
	}
	
	public void createNewUri(String newUri){
		setURI(newUri);
		setDcUri(newUri);
		this.setChanged();
		notifyObservers();
	}

	public void setSelectedLanguage(String selectedLanguage) {
		this.selectedLanguage = selectedLanguage;
	}

	public String getSelectedLanguage() {
		return selectedLanguage;
	}

	public void setFormat(String format) {
		this.format = format;
	}

	public String getFormat() {
		return format;
	}
	
	public String getDcUri(){
		return dcModel.getURI();
	}
	
	public DCModel getDcModel(){
		return dcModel;
	}
	
	public void setDcUri(String uri){
		dcModel.setURI(uri);
	}
}
